/* man.cs - Manual pages implementation, some basic help
 * Part of CalcSharp project.
 * This is Free Software! See GNU GPL for more details
 * CalcSharp (c) 2008 Webmaster442
 */
using System.Data;
using System.Globalization;
using System.Text;
using CalcSharp.Core;
using CalcSharp.Core.IO;
using System.Collections.Generic;
using System;

namespace CalcSharp
{
    class manual
    {
        public static string getManEntry(string cmdname)
        {
            string text = "-";
            StringBuilder html = new StringBuilder(Properties.Resources.manpage_default);
            if (Program.loaded.isCommandAvailable(cmdname))
            {
                text = Program.loaded.GetCMDPluginIface(cmdname).Help;
            }
            if (text == "-")
            {
                DataSet manpages = new DataSet();
                manpages.ReadXml(FileHelper.Appdir + @"\manual.xml");
                for (int i = 0; i < manpages.Tables[0].Rows.Count; i++)
                {
                    if (manpages.Tables[0].Rows[i][0].ToString().ToUpper(CultureInfo.CurrentCulture) == cmdname.ToUpper(CultureInfo.CurrentCulture))
                    {
                        text = manpages.Tables[0].Rows[i][1].ToString().Replace("\t", "");
                        break;
                    }
                }
                manpages.Dispose();
            }
            html = html.Replace("<%CMDNAME%>", cmdname);
            if (text != "-")
            {
                html = html.Replace("<%ENTRY%>", text.Trim());
                return html.ToString();
            }
            else throw new CSException("No manual entry available for " + cmdname);

        }
        public static string[] getdocumented()
        {
            int i, count;
            DataSet manpages = new DataSet();
            manpages.ReadXml(FileHelper.Appdir + @"\manual.xml");
            count = manpages.Tables[0].Rows.Count;
            List<string> documented = new List<string>();
            for (i = 0; i < count; i++)
            {
                documented.Add(manpages.Tables[0].Rows[i][0].ToString().ToUpper(CultureInfo.CurrentCulture));
            }
            return documented.ToArray();
        }

        public static string[] getdocumentedplugins()
        {
            string help;
            int i;
            List<string> documented = new List<string>();
            string[] backup = Program.cmds;
            for (i = 0; i < Program.loaded.CMDCount; i++)
            {
                try
                {
                    Program.cmds = new string[] { Program.loaded[i, PlugType.Command] };
                    help = Program.loaded.GetCMDPluginIface(Program.loaded[i, PlugType.Command]).Help;
                    if (!string.IsNullOrEmpty(help)) documented.Add(Program.loaded[i, PlugType.Command]);
                }
                catch (Exception) { }
            }
            for (i = 0; i < Program.loaded.WinCount; i++)
            {
                try
                {
                    Program.cmds = new string[] { Program.loaded[i, PlugType.Window] };
                    help = Program.loaded.GetCMDPluginIface(Program.loaded[i, PlugType.Window]).Help;
                    if (!string.IsNullOrEmpty(help)) documented.Add(Program.loaded[i, PlugType.Window]);
                }
                catch (Exception) { }
            }
            Program.cmds = backup;
            help = null;

            return documented.ToArray();
        }
    }
}